package com.tbit.uqbike.service.business.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tbit.uqbike.object.pojo.AccountUser;
import com.tbit.uqbike.object.pojo.UserPhoneLog;
import com.tbit.uqbike.service.business.UserPhoneLogService;
import com.tbit.uqbike.webmanager.dao.core.AccountUserDao;
import com.tbit.uqbike.webmanager.dao.log.UserPhoneLogDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * 用户手机号码变动日志Service业务层处理
 *
 * @author jing
 * @date 2021-04-02
 */
@Service
public class UserPhoneLogServiceImpl implements UserPhoneLogService {
    @Autowired
    private UserPhoneLogDao userPhoneLogDao;
    @Autowired
    private AccountUserDao accountUserDao;

    /**
     * 查询用户手机号码变动日志列表
     *
     * @param userPhoneLog 用户手机号码变动日志
     * @return 用户手机号码变动日志
     */
    @Override
    public PageInfo<UserPhoneLog> selectUserPhoneLogListPage(UserPhoneLog userPhoneLog) {
        buildParams(userPhoneLog);
        PageInfo<UserPhoneLog> pageInfo = PageHelper.startPage(userPhoneLog.getPageNO(), userPhoneLog.getRowCount()).doSelectPageInfo(() -> userPhoneLogDao.selectUserPhoneLogList(userPhoneLog));
        if (!CollectionUtils.isEmpty(pageInfo.getList())){
            fillPhone(pageInfo.getList());
        }
        return pageInfo;
    }

    @Override
    public List<UserPhoneLog> selectUserPhoneLogList(UserPhoneLog userPhoneLog) {
        buildParams(userPhoneLog);
        List<UserPhoneLog> userPhoneLogList = userPhoneLogDao.selectUserPhoneLogList(userPhoneLog);
        if (!CollectionUtils.isEmpty(userPhoneLogList)){
            fillPhone(userPhoneLogList);
        }
        return userPhoneLogList;
    }

    /**
     * 根据用户号码填充操作人账号id
     *
     * @param userPhoneLog 用户手机号码变动日志对象
     */
    private void buildParams(UserPhoneLog userPhoneLog) {
        List<AccountUser> accountUser = accountUserDao.getAccountUserByPhone(userPhoneLog.getOperaPhone(), "86");
        List<Integer> operaUserIdList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(accountUser)) {
            for (AccountUser user : accountUser) {
                operaUserIdList.add(user.getAccountUserId());
            }
            userPhoneLog.setOperaUserIdList(operaUserIdList);
        }
    }
    /**
     * 根据操作人账号id填充用户号码
     *
     * @param userPhoneLogs 用户手机号码变动日志对象集合
     */
    private void fillPhone(List<UserPhoneLog> userPhoneLogs) {
        if (CollectionUtils.isEmpty(userPhoneLogs)) {
            return;
        }

        List<Integer> userIds = new ArrayList<>();
        for (UserPhoneLog userPhoneLog : userPhoneLogs) {
            Integer operaUserId = userPhoneLog.getOperaUserId();
            if (operaUserId != null) {
                userIds.add(operaUserId);
            }
        }

        // 获取用户信息并转换为Map以便快速查找
        List<AccountUser> accountUserByIds = accountUserDao.getAccountUserByIds(userIds);
        Map<Integer, AccountUser> accountUserMap = new HashMap<>();
        for (AccountUser accountUser : accountUserByIds) {
            if (accountUser != null && accountUser.getAccountUserId() != null) {
                accountUserMap.put(accountUser.getAccountUserId(), accountUser);
            }
        }

        // 填充电话号码
        for (UserPhoneLog userPhoneLog : userPhoneLogs) {
            Integer operaUserId = userPhoneLog.getOperaUserId();
            if (operaUserId != null && accountUserMap.containsKey(operaUserId)) {
                userPhoneLog.setOperaPhone(accountUserMap.get(operaUserId).getPhone());
            }
        }
    }

    /**
     * 新增用户手机号码变动日志
     *
     * @param userPhoneLog 用户手机号码变动日志
     * @return 结果
     */
    @Override
    public int insertUserPhoneLog(UserPhoneLog userPhoneLog) {
        return userPhoneLogDao.insertUserPhoneLog(userPhoneLog);
    }


    /**
     * 批量删除用户手机号码变动日志
     *
     * @param userIds 需要删除的用户手机号码变动日志ID
     * @return 结果
     */
    @Override
    public int deleteUserPhoneLogByIds(Integer[] userIds) {
        return userPhoneLogDao.deleteUserPhoneLogByIds(userIds);
    }

    /**
     * 删除用户手机号码变动日志信息
     *
     * @param userId 用户手机号码变动日志ID
     * @return 结果
     */
    @Override
    public int deleteUserPhoneLogById(Integer userId) {
        return userPhoneLogDao.deleteUserPhoneLogById(userId);
    }
}
